package com.wyk.springbootdemo.controller;

import com.wyk.springbootdemo.AsyncTest.AsyncTest;
import com.wyk.springbootdemo.entity.MyException;
import com.wyk.springbootdemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;

@RestController
public class controller {
    @RequestMapping("/hello")
    public String hello(){

        return "hello!world";

    }

//    @RequestMapping(path ="/{id}",method = RequestMethod.GET)
//    public String getId(@PathVariable("id") String id){
//        return id;
//}

    @RequestMapping(path ="/getUser",method = RequestMethod.GET)
    public String getUser(User user){
        return user.getId()+"/////"+user.getName();
    }

    Map<String,Object> params = new HashMap<>();
    @RequestMapping(value = "/page_user")
    public Object pageUser(@RequestParam(defaultValue = "0",name = "pageNo") int pageNo,
                           @RequestParam(defaultValue = "3",name = "pageSize") int pageSize){
        if(pageNo==0){
            throw new MyException(999,"这是一个异常");
        }
        params.clear();
        params.put("pageNo",pageNo);
        params.put("pageSize",pageSize);
        return params;
    }

    @RequestMapping(path = "/saveUser",method = RequestMethod.POST)
    public Object saveUser(@RequestBody User user){
        params.clear();
        params.put("user" ,user);
        return params;

    }
    @GetMapping("get_header")
    public Object getHeader(@RequestHeader("access_token") String accessToken ,String id,HttpServletRequest request){
      params.clear();
      params.put("access_token",accessToken);
      params.put("id",id);
        params.put("session",request.getSession().getId());
        return params;
    }
    @PutMapping("/putXX")
    public Object put(String id){
        params.clear();
        params.put("id",id);
        return params;
    }

    @DeleteMapping("/delete")
    public Object delete(String id){
        params.clear();
        params.put("id",id);
        return params;
    }

    @RequestMapping(path ="/get_User",method = RequestMethod.GET)
    public Object getUser1(User user){
        user.setBrithday(new Date());
        return user;
    }

    @Autowired
    private AsyncTest asyncTest;
    @RequestMapping("/testAcync")
    public String testAcync()throws  InterruptedException{
        //  获取当前系统时间
        long begin =System.currentTimeMillis();
        Future<String> test1= asyncTest.test01();
        Future<String> test2= asyncTest.test02();

        for (;;) {
            if(test1.isDone()&&test2.isDone()){
                break;
            }
        }

        //  获取当前系统时间
        long end =System.currentTimeMillis();

        System.out.println("总耗时："+(end-begin));
        return "ok";
    }
}
